<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeySession lifetime after release() without references</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // MediaKeySessions remain as long as:
            //   JavaScript has a reference to it
            //   OR (MediaKeys is around
            //       AND the session has not received a close() event)
            promise_test(function(test)
            {
                var initDataType;
                var initData;

                // Create 2 sessions.
                var mediaKeys;
                var mediaKeySession1;
                var mediaKeySession2;

                // Even though there should be no existing objects, start by
                // running gc() and verifying that none exist. This also
                // allows the failures to be reported from inside a promise
                // so that the test fails properly.
                return createGCPromise().then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After initial gc()');

                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration());
                }).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(result) {
                    mediaKeys = result;
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'MediaKeys.create()');

                    mediaKeySession1 = mediaKeys.createSession();
                    return mediaKeySession1.generateRequest(initDataType, initData);
                }).then(function() {
                    assert_true(mediaKeySession1.sessionId && mediaKeySession1.sessionId.length > 0);
                    verifyMediaKeyAndMediaKeySessionCount(1,  1, 'MediaKeys.createSession(1)');

                    mediaKeySession2 = mediaKeys.createSession();
                    return mediaKeySession2.generateRequest(initDataType, initData);
                }).then(function() {
                    assert_true(mediaKeySession2.sessionId && mediaKeySession2.sessionId.length > 0);
                    verifyMediaKeyAndMediaKeySessionCount(1, 2, 'mediaKeys.createSession(2)');

                    // Run gc(). All sessions should remain as we have a
                    // reference to each one.
                    return createGCPromise();
                }).then(function(result) {
                    verifyMediaKeyAndMediaKeySessionCount(1, 2, 'After gc()');

                    // Close the sessions. Once the close() event is received,
                    // they should get garbage collected as there are no JS
                    // references to them.
                    var promise = mediaKeySession1.close();
                    mediaKeySession1 = null;
                    return promise;
                }).then(function(result) {
                    // Give time so that the close event can be processed by
                    // MediaKeySession.
                    return delayToAllowEventProcessingPromise();
                }).then(function(result) {
                    return createGCPromise();
                }).then(function(result) {
                    verifyMediaKeyAndMediaKeySessionCount(1, 1, 'mediaKeySession1 not collected');

                    var promise = mediaKeySession2.close();
                    mediaKeySession2 = null;
                    return promise;
                }).then(function(result) {
                    // Provide time for the mediaKeySession2 close event to be
                    // handled.
                    return delayToAllowEventProcessingPromise();
                }).then(function(result) {
                    return createGCPromise();
                }).then(function(result) {
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'mediaKeySession2 not collected');
                    assert_not_equals(mediaKeys, null);
                });
            }, 'MediaKeySession lifetime after release() without references');
        </script>
    </body>
</html>
